//
//  LMChangeNameController.m
//  舍品
//
//  Created by 楼 眉 on 16/3/23.
//  Copyright © 2016年 楼 眉. All rights reserved.
//
static         NSInteger           const                KNameTextFieldNum           =           9;
static    NSString    *const    KUserInformationChange   =   @"userInformationChange";

#import "LMChangeNameController.h"
#import "LMWarningLabel.h"

@interface LMChangeNameController ()<UITextFieldDelegate>

@property (nonatomic,weak)  UITextField *tf;

@end

@implementation LMChangeNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    [self setupUi];
    
}
- (void)setupUi
{
    LMUser *user = [LMUser currentUser];
    self.view.backgroundColor = [UIColor lightGrayColor];
    UITextField *tf = [[UITextField alloc]init];
    tf.textColor = [UIColor lightGrayColor];
    tf.text = user.nickname;
    [self.view addSubview:tf];
    self.tf = tf;
    tf.delegate = self;
    tf.frame = CGRectMake( 20 *kUnit, CGRectGetMaxY(self.navigationController.navigationBar.frame) + 10*kUnit, SCREEN_WIDTH - 40*kUnit, 44*kUnit);
    tf.backgroundColor = [UIColor whiteColor];
    
    UILabel *warningLabel = [[UILabel alloc]init];
    [self.view addSubview:warningLabel];
    warningLabel.frame = CGRectMake(20*kUnit, CGRectGetMaxY(tf.frame) + 20*kUnit, SCREEN_WIDTH - 40*kUnit, 33*kUnit);
    warningLabel.textAlignment = NSTextAlignmentCenter;
    warningLabel.textColor = [UIColor whiteColor];
    warningLabel.text = [NSString stringWithFormat:@"最多输入%ld个字符",(long)KNameTextFieldNum];
    
    UIButton *btn = [[UIButton alloc]init];
    [self.view addSubview:btn];
    btn.backgroundColor = RGBCOLOR(39, 177, 235);
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    btn.frame = CGRectMake(tf.x, CGRectGetMaxY(warningLabel.frame) + 20*kUnit, tf.width, tf.height);
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 保存按钮点击
- (void)btnClick
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    if (manager.isReachable == NO) {
        [LMWarningLabel LM_createWarningLabelWithTitle:nil];
    }else
    {
        if (self.tf.text.length <=0) {
            [LMWarningLabel LM_createWarningLabelWithTitle:@"请输入昵称"];
            return;
        }
        [self.view endEditing:YES];
        [SVProgressHUD showWithStatus:@"提交中"];
        
        LMUser *user = [LMUser currentUser];
        NSString *oldName = user.nickname;
        user.nickname = self.tf.text;
        __weak typeof(self) weakSelf = self;
        [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                [SVProgressHUD dismiss];
                [LMWarningLabel LM_createWarningLabelWithTitle:@"修改昵称成功"];
                [[NSNotificationCenter defaultCenter] postNotificationName:KUserInformationChange object:nil];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                [SVProgressHUD dismiss];
                [LMWarningLabel LM_createWarningLabelWithTitle:@"修改失败"];
                user.nickname = oldName;
                weakSelf.tf.text = user.nickname;
            }
        }];
    }
}

#pragma mark - 规定能输入字符串的个数
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= KNameTextFieldNum && range.length == 0) {
        return NO;
    }
    return YES;
}
#pragma mark - didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - dealloc
- (void)dealloc
{
    NSLog(@"dealloc--changeNameVc");
}
@end
